package com.hx.localmiaosha.service.imp;

import com.alibaba.fastjson.JSON;
import com.hx.common.R;
import com.hx.common.constant.GoodServerConstant;
import com.hx.common.domain.MiaoshaGoods;
import com.hx.common.domain.MiaoshaOrder;
import com.hx.common.domain.MiaoshaUser;
import com.hx.common.domain.OrderInfo;
import com.hx.common.to.OrderMessageTo;
import com.hx.common.to.RedisGoodsTo;
import com.hx.common.vo.GoodsVo;
import com.hx.localmiaosha.dao.LocalOrderDao;

import com.hx.localmiaosha.service.LocalMiaoshaGoodsService;
import com.hx.localmiaosha.service.LocalMiaoshaUserService;
import com.hx.localmiaosha.service.LocalOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author wonghiu
 * @Date 2021/6/6 11:00
 * @Version 1.0.1
 */
@Component
@Slf4j
public class LocalOrderServiceImp implements LocalOrderService {
    @Autowired
    private LocalOrderDao orderDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private LocalMiaoshaGoodsService miaoshaGoodsService;

    @Autowired
    private LocalMiaoshaUserService miaoshaUserService;

    @Override
    public void createMiaoShaOrder(OrderMessageTo orderMessageTo) {
        MiaoshaOrder miaoshaOrder = new MiaoshaOrder();
        BeanUtils.copyProperties(orderMessageTo,miaoshaOrder);
        // 设置秒杀单状态为【新建】
        miaoshaOrder.setState(0);
        orderDao.insertMiaoshaOrder(miaoshaOrder);
        // 发送到定时队列，启动定时关单功能
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",miaoshaOrder);

    }
    @Override
    public void createPreOrder(MiaoshaUser miaoshaUser, MiaoshaGoods miaoshaGoods,OrderMessageTo orderMessageTo) {
        // 从缓存中获取秒杀商品的信息
        String json = redisTemplate.opsForValue().get(GoodServerConstant.GOODS_INFO_PREFIX + orderMessageTo.getGoodsId());
        GoodsVo goodsVo = JSON.parseObject(json, GoodsVo.class);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(miaoshaUser.getId());
        orderInfo.setGoodsId(miaoshaGoods.getGoodsId());
        orderInfo.setCreateDate(new Date());
        orderInfo.setGoodsCount(orderMessageTo.getCountGoods());
        orderInfo.setOrderId(orderMessageTo.getOrderId());
        orderInfo.setGoodsName(goodsVo.getGoodsName());
        orderInfo.setGoodsPrice(orderMessageTo.getCountGoods()*miaoshaGoods.getMiaoshaPrice());

        // 设置订单状态为【新建】
        orderInfo.setStatus(0);

        orderDao.insert(orderInfo);
        // 发送到定时队列，启动定时关单功能
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderMessageTo);

    }

    @Override
    @Transactional
    public boolean createPreOrderNoCache(OrderMessageTo orderMessageTo) {
        Long goodsId = orderMessageTo.getGoodsId();
        Long userId = orderMessageTo.getUserId();

        GoodsVo goods = miaoshaGoodsService.getGoodsVoByGoodsId(goodsId);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userId);
        orderInfo.setGoodsId(goodsId);
        orderInfo.setCreateDate(new Date());
        orderInfo.setGoodsCount(orderMessageTo.getCountGoods());
        orderInfo.setOrderId(orderMessageTo.getOrderId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(orderMessageTo.getCountGoods()*goods.getMiaoshaPrice());

        // 设置订单状态为【新建】
        orderInfo.setStatus(0);

        orderDao.insert(orderInfo);
        log.info("创建预订单完成");
        return true;
    }
    @Override
    public boolean createPreOrderWithCache(OrderMessageTo orderMessageTo) {
        Long goodsId = orderMessageTo.getGoodsId();
        Long userId = orderMessageTo.getUserId();

        String json = redisTemplate.opsForValue().get(GoodServerConstant.GOODS_INFO_PREFIX + goodsId);
        RedisGoodsTo goods = JSON.parseObject(json, RedisGoodsTo.class);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userId);
        orderInfo.setGoodsId(goodsId);
        orderInfo.setCreateDate(new Date());
        orderInfo.setGoodsCount(orderMessageTo.getCountGoods());
        orderInfo.setOrderId(orderMessageTo.getOrderId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(orderMessageTo.getCountGoods()*goods.getMiaoshaPrice());

        // 设置订单状态为【新建】
        orderInfo.setStatus(0);

        orderDao.insert(orderInfo);
        log.info("创建预订单完成");
        return true;
    }
    @Override
    public void updateMiaoshaOrderStateById(Long orderId,Integer stateId) {
        orderDao.updateMiaoshaOrderState(orderId,stateId);
    }

    @Override
    public void updateOrderStatusById(Long orderId,Integer stateId) {
        orderDao.updateOrderStatus(orderId,stateId);
    }
    @Override
    public OrderInfo getOrderById(Long orderId) {
        return orderDao.getOrderByOrderId(orderId);
    }


    @Override
    public R check(Long orderId) {
        return null;
    }

    @Override
    public Integer getOrderStateById(Long orderId) {
        OrderInfo orderById = orderDao.getOrderByOrderId(orderId);
        return orderById.getStatus();
    }

    @Override
    @Transactional
    // 更新订单状态为1
    public boolean finishPreOrder(OrderMessageTo orderMessageTo) {
        orderDao.updateOrderStatus(orderMessageTo.getOrderId(),1);
        log.info("完成订单创建");
        return true;
    }
}
